import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Put,
  Query,
} from '@nestjs/common';
import { RolesService } from './roles.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { Serialize } from '@/common/decorator/serialize.decorator';
import { ResultRoleConverterDto } from './dto/result-converter.dto';
import { IdsDto } from '@/common/dto/ids.dto';
import { SearchDto } from '@/common/dto/base-search.dto';

@Controller('roles')
@ApiTags('创建角色')
export class RolesController {
  constructor(private readonly rolesService: RolesService) {}

  @Post()
  @ApiOperation({ summary: '创建角色' })
  @Serialize(ResultRoleConverterDto, false)
  create(@Body() createRoleDto: CreateRoleDto) {
    return this.rolesService.create(createRoleDto);
  }

  @Get('page')
  @ApiOperation({ summary: '分页查询角色' })
  findAll(@Query() query: SearchDto) {
    return this.rolesService.findAll(query);
  }

  @Get(':id/form')
  @ApiOperation({ summary: '根据ID查询角色' })
  findOne(@Param('id') id: string) {
    return this.rolesService.findOne(id);
  }

  @Put(':id')
  @ApiOperation({ summary: '更新角色' })
  update(@Param('id') id: string, @Body() updateRoleDto: UpdateRoleDto) {
    return this.rolesService.update(id, updateRoleDto);
  }

  @Delete(':ids')
  @ApiOperation({ summary: '删除角色' })
  remove(@Param() ids: IdsDto) {
    return this.rolesService.remove(ids);
  }

  @Get('options')
  @ApiOperation({ summary: '获取角色下拉列表' })
  getOptions() {
    return this.rolesService.getOptions();
  }

  @Put(':id/status')
  @ApiOperation({ summary: '更新角色状态' })
  updateStatus(@Param('id') id: string, @Query() query: { status: number }) {
    return this.rolesService.updateStatus(id, query.status);
  }

  @Put(':id/menus')
  @ApiOperation({ summary: '分配菜单给角色' })
  updateMenus(@Param('id') id: string, @Body() body: string[]) {
    return this.rolesService.updateRoleMenus(id, body);
  }

  @Get(':id/menuIds')
  @ApiOperation({ summary: '获取角色菜单ID集合' })
  getMenuIds(@Param('id') id: string) {
    return this.rolesService.getRoleMenuIds(id);
  }
}
